use salvo::{handler, Request, Response};
use salvo::http::StatusCode;

#[handler]
pub async fn test_method() -> String {
    "原始响应体".to_string()
}
#[handler]
pub async fn user_list() -> String {
    "user list".to_string()
}
#[handler]
pub async fn user_post() -> String {
    "user post".to_string()
}
#[handler]
pub async fn order_list() -> String {
    "order list".to_string()
}
#[handler]
pub async fn order_list_param(req: &mut Request, res: &mut Response) {
    match req.query::<String>("key") {
        Some(key) => {
            let body = format!("key:{}", key);
            res.body(body);
        }
        None => {
            res.status_code(StatusCode::INTERNAL_SERVER_ERROR).body("缺少key参数1");
        }
    }
}
#[handler]
pub async fn order_list_path_param(req: &mut Request, res: &mut Response) {
    match req.param::<String>("key") {
        Some(key) => {
            let body = format!("key:{}", key);
            res.status_code(StatusCode::OK).body(body);
        }
        None => {
            res.status_code(StatusCode::INTERNAL_SERVER_ERROR).body("缺少key参数2");
        }
    }
}


#[cfg(test)]
mod tests {
    use salvo::Service;
    use salvo::test::{ResponseExt, TestClient};
    use tokio::test;
    use crate::router;

    #[test]
    async fn test_user_list() {
        let service = Service::new(router());
        let content = TestClient::get(format!("http://127.0.0.1:14300/user"))
            .send(&service).await.take_string().await.unwrap();
        assert_eq!(content, "user list");
    }
    #[test]
    async fn test_user_post() {
        let service = Service::new(router());
        let content = TestClient::post(format!("http://127.0.0.1:14300/user"))
            .send(&service).await.take_string().await.unwrap();
        assert_eq!(content, "user post");
    }
    #[test]
    async fn test_order_list() {
        let service = Service::new(router());
        let content = TestClient::post(format!("http://127.0.0.1:14300/order"))
            .send(&service).await;
        assert_eq!(405, content.status_code.unwrap().as_u16());

        let mut content = TestClient::get(format!("http://127.0.0.1:14300/order"))
            .send(&service).await;
        assert_eq!(500, content.status_code.unwrap().as_u16());
        let content = content.take_string().await.unwrap();
        assert_eq!(content, "缺少key参数1");

        let content = TestClient::get(format!("http://127.0.0.1:14300/order_list"))
            .send(&service).await.take_string().await.unwrap();
        assert_eq!(content, "order list");
    }
    #[test]
    async fn test_user_list_with_param() {
        let service = Service::new(router());

        let mut resp = TestClient::get(format!("http://127.0.0.1:14300/order"))
            .send(&service).await;
        assert_eq!(500, resp.status_code.unwrap().as_u16());
        let content = resp.take_string().await.unwrap();
        assert_eq!("缺少key参数1", content);

        let content = TestClient::get(format!("http://127.0.0.1:14300/order?key=哈哈哈1"))
            .send(&service).await.take_string().await.unwrap();
        assert_eq!("key:哈哈哈1", content);

        let content = TestClient::get(format!("http://127.0.0.1:14300/order/哈哈哈"))
            .send(&service).await.take_string().await.unwrap();
        assert_eq!(content, "key:哈哈哈");
    }
}